package com.tianyu.service.mqtt.handler.inbound.next;

import com.alibaba.fastjson2.JSONObject;
import com.tianyu.service.mqtt.common.MqttMessageChannel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.mqtt.support.MqttHeaders;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.stereotype.Component;

import java.util.Objects;


/**
 * @author swyan
 */
@Component
@Slf4j
public class InstructResHandler {

    @ServiceActivator(inputChannel = MqttMessageChannel.ChannelName.INBOUND_INSTRUCT_RES)
    protected void messageReceived(Message<String> message) {
        MessageHeaders headers = message.getHeaders();
        String topic = Objects.requireNonNull(headers.get(MqttHeaders.RECEIVED_TOPIC)).toString();
        final String companyId = topic.split("/")[3];
        final String deviceSn = topic.split("/")[4];
        String content = message.getPayload();
        final JSONObject jsonObject = JSONObject.parseObject(content);
        log.info(message.toString());
    }
}
